package com.ycfmedical.bed.present;


import android.content.Context;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.ycfmedical.bed.local.DataManager;
import com.ycfmedical.bed.local.entity.TimerReminder;
import com.ycfmedical.bed.ui.fragment.TimerReminderFragment;
import com.ycfmedical.bed.utils.AlarmManagerUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XPresent;

/**
 * Created by Administrator on 2017-04-13.
 * 定时提醒
 */
public class TimerReminderPresenter extends XPresent<TimerReminderFragment>
{
    private TimePickerView pvTime;
    Context context;

    public TimerReminderPresenter(Context context)
    {
        this.context = context;
    }

    public void TimePicker() {
        pvTime = new TimePickerView(context, TimePickerView.Type.HOURS_MINS);
        pvTime.setTime(new Date());
        pvTime.setCyclic(false);
        pvTime.setCancelable(true);
        // 时间选择后回调
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener()
        {
            @Override
            public void onTimeSelect(Date date)
            {
                //获取已经设置好的时间并设置进闹钟
                String time = getTime(date);
                getV().showToast(time);
                setClock(time);
                //在list中增添数据
                addLoadData(time);
            }
        });
        //展示时间设置框
        pvTime.show();
    }

    private void addLoadData(String showTimes)
    {
        TimerReminder timerR = new TimerReminder();
        timerR.setReminderCounts(1);
        timerR.setReminderTime(showTimes);
        List<TimerReminder> timerLists = DataManager.getInstance().getmTimerList();
        timerLists.add(timerR);
        DataManager.getInstance().setmTimerList(timerLists);
        getV().showData();
    }

    private String getTime(Date date)
    {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(date);
    }


    /**
     * 设置闹钟方法
     * @param setingTimes
     */
    public void setClock(String setingTimes)
    {
        if (setingTimes != null && setingTimes.length() > 0)
        {
            String[] times = setingTimes.split(":");
            AlarmManagerUtil.setAlarm(context,
                    0,
                    Integer.parseInt(times[0]),
                    Integer.parseInt(times[1]),
                    0,
                    0,
                    "闹钟响了",
                    0);
            Toast.makeText(context, "闹钟设置成功", Toast.LENGTH_LONG).show();
        }

    }
}